package cn.tansanqinger.dao.sql.chatroom;

import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Repository;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;

import cn.tansanqinger.dao.sql.IChatMessageSqlDao;
import cn.tansanqinger.pojo.Chatmessage;

@Repository
public class ChatMessageSqlDaoImpl {

	@Autowired
	private IChatMessageSqlDao dao;
	
	/**
	 * 查询数据（十条）
	 * @param saccount
	 * @param faccount
	 * @param page
	 * @return
	 */
	public List<Chatmessage> selectChatmessages(String saccount, String faccount,int page){
		QueryWrapper<Chatmessage> queryWrapper = new QueryWrapper<Chatmessage>();
		queryWrapper.lambda().eq(Chatmessage::getFaccount, faccount).eq(Chatmessage::getSaccount, saccount).or();
		queryWrapper.lambda().eq(Chatmessage::getFaccount, saccount).eq(Chatmessage::getSaccount, faccount);
		Page<Chatmessage> pg = new Page<Chatmessage>(1, 10);
		IPage<Chatmessage> iPage = dao.selectPage(pg, queryWrapper);
		return iPage.getRecords();
	}
	
	/**
	 * 初始显示的数据
	 * @param saccount
	 * @param faccount
	 * @return
	 */
	public Chatmessage selectChatmessagesOne(String saccount, String faccount){
		QueryWrapper<Chatmessage> queryWrapper = new QueryWrapper<Chatmessage>();
		queryWrapper.lambda().eq(Chatmessage::getFaccount, faccount).eq(Chatmessage::getSaccount, saccount).or();
		queryWrapper.lambda().eq(Chatmessage::getFaccount, saccount).eq(Chatmessage::getSaccount, faccount);
		queryWrapper.orderByDesc("ctime").last("limit 1");
		return dao.selectOne(queryWrapper);
	}
	
	/**
	 * 数据条数
	 * @param saccount
	 * @param faccount
	 * @return
	 */
	public int selectChatNumber(String saccount, String faccount) {
		QueryWrapper<Chatmessage> queryWrapper = new QueryWrapper<Chatmessage>();
		queryWrapper.lambda().eq(Chatmessage::getFaccount, faccount).eq(Chatmessage::getSaccount, saccount).or();
		queryWrapper.lambda().eq(Chatmessage::getFaccount, saccount).eq(Chatmessage::getSaccount, faccount);
		return dao.selectCount(queryWrapper);
	}
	
	/**
	 * 删除数据
	 * @param time
	 */
	public void delete(Long time) {
		QueryWrapper<Chatmessage> queryWrapper = new QueryWrapper<Chatmessage>();
		queryWrapper.le("ctime", time);
		dao.delete(queryWrapper);
	}
	
	/**
	 * 添加数据
	 * @param chatmessages
	 */
	public void add(List<Chatmessage> chatmessages) {
		for (int i = 0; i < chatmessages.size(); i++) {
			dao.insert(chatmessages.get(i));
		}
		
	}
}
